<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button  @click="handleEdit({name:''})"
          >添加广告位</el-button
        >
      </div>
      <el-table
      :data="spaces"
      style="width: 100%">
      <el-table-column
        label="spaceKey"
        prop="spaceKey"
        align="center">
      </el-table-column>
      <el-table-column
        prop="name"
        label="广告位名称"
        align="center">
      </el-table-column>

      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center">
      </el-table-column>

      <el-table-column
        prop="updateTime"
        label="更新时间"
        align="center">
      </el-table-column>

      <el-table-column
        label="操作"
        align="center">
        <template slot-scope="scope">
             <el-link @click="handleEdit(scope.row)">编辑</el-link>
        </template>
      </el-table-column>
    </el-table>

    </el-card>

    <el-dialog
        :visible.sync="dialogVisible"
        width="50%">
        <el-form ref="form" :model="form" label-width="100px">

  <el-form-item label="广告位名称">
    <el-input  v-model="form.name"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="onSubmit">确定</el-button>
  </el-form-item>
</el-form>

</el-dialog>

  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { getAllSpaces, saveOrUpdate } from '@/service/advert-space'
export default Vue.extend({
  name: 'MenuCreate',
  data () {
    return {
      spaces: [],
      dialogVisible: false,
      form: {
        name: '',
        id: null
      }
    }
  },
  created () {
    this.loadAllMenu()
  },
  methods: {
    async loadAllMenu () {
      const { data } = await getAllSpaces()
      if (data.state === 1) {
        this.spaces = data.content
      }
    },
    handleEdit (row:any) {
      this.form.name = row.name
      this.form.id = row.id
      this.dialogVisible = true
    },
    async onSubmit () {
      const { data } = await saveOrUpdate(this.form)
      if (data.state === 1) {
        this.$message.success(data.message)
        this.dialogVisible = false
      } else {
        this.$message.error(data.message)
      }
    }

  }
})
</script>
<style scoped>

</style>
